﻿Imports XL.Common
Imports XL.Common.Utils

Public Class ServiceTK
    Inherits XL.DB.Utils.BaseService


    Public Shared Function GetInstance() As ServiceTK

        Dim result As ServiceTK
        If AssetVisionXAuto.Decls.CURRENT_DB_TYPE = XL.DB.DBDecl.DBType.Remoting Then

            result = CType(Activator.GetObject(GetType(ServiceTK), _
                String.Format("tcp://{0}/ServiceTK.remote", _
                    SysInfo.ReadLocalSysInfo(CommDecl.XLSLVN_SYSTEM_REMOTE_SERVICE_URL))),  _
                ServiceTK)

            If result.ValidateAuthPassword(CommDecl.CURRENT_LOCAL_REMOTE_AUTH_PASSWORD) = False Then
                Dim ex As New XLException(XLException.ErrorClassify.ReadDataError)
                ex.SetMessage(CommDecl.MSG_ALERT_REMOTE_AUTH_DENIED)
                Throw ex
            End If

        Else

            result = New ServiceTK
        End If

        Return result

    End Function


    Public Sub ServInitDefaultCommonItem( _
        ByRef refDEFAULT_CI_VALUE_ELIMINATE_WAY_MAYBE_LOST As Integer, _
        ByRef refDEFAULT_CI_VALUE_ELIMINATE_WAY_NONE As Integer, _
        ByRef refDEFAULT_CI_VALUE_ELIMINATE_WAY_LOST As Integer, _
        ByRef refDEFAULT_CI_VALUE_IN_ACCOUNT_CREDENCE_SETTLED As Integer, _
        ByRef refDEFAULT_CI_VALUE_PURCHASE_WAY As Integer, _
        ByRef refDEFAULT_CI_VALUE_GUARANTEE_EXPIRE_TRANSACTION As Integer, _
        ByRef refDEFAULT_CI_VALUE_OWNER_TYPE_PROPRIETOR As Integer, _
        ByRef refRegisterVersionLevelString As String _
        )


        Dim itemID As String
        Dim eliminateWayConditions As New AssetVisionXAuto.Facade.AfCIV.ConditionOfCIV_ELIMINATE_WAY(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        Dim eliminateWayRow As AssetVisionXAuto.FTs.FT_CIV_ELIMINATE_WAYRow

        eliminateWayConditions.Clear()
        eliminateWayConditions.Add(AssetVisionXAuto.Facade.AfCIV.CIV_ELIMINATE_WAYColumns.ITEM_MEANINGColumn, " =", AssetVisionXService.Decls.DEFAULT_CI_TEXT_ELIMINATE_WAY_MAYBE_LOST)
        eliminateWayRow = AssetVisionXAuto.Facade.AfCIV.GetCIV_ELIMINATE_WAYRow(eliminateWayConditions)

        If IsNothing(eliminateWayRow) = True Then
            itemID = System.Guid.NewGuid.ToString()
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_MAYBE_LOST = _
                AssetVisionXService.Facade.OpSysConfig.CreateCommonItem( _
                    itemID, _
                    AssetVisionXAuto.Decls.CICNAME_ELIMINATE_WAY, _
                    AssetVisionXService.Decls.DEFAULT_CI_TEXT_ELIMINATE_WAY_MAYBE_LOST, _
                    String.Empty, _
                    True)
        Else
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_MAYBE_LOST = eliminateWayRow.ITEM_VALUE
        End If

        eliminateWayConditions.Clear()
        eliminateWayConditions.Add(AssetVisionXAuto.Facade.AfCIV.CIV_ELIMINATE_WAYColumns.ITEM_MEANINGColumn, " =", AssetVisionXService.Decls.DEFAULT_CI_TEXT_ELIMINATE_WAY_NONE)
        eliminateWayRow = AssetVisionXAuto.Facade.AfCIV.GetCIV_ELIMINATE_WAYRow(eliminateWayConditions)

        If IsNothing(eliminateWayRow) = True Then
            itemID = System.Guid.NewGuid.ToString()
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_NONE = _
                AssetVisionXService.Facade.OpSysConfig.CreateCommonItem( _
                    itemID, _
                    AssetVisionXAuto.Decls.CICNAME_ELIMINATE_WAY, _
                    AssetVisionXService.Decls.DEFAULT_CI_TEXT_ELIMINATE_WAY_NONE, _
                    String.Empty, _
                    True)
        Else
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_NONE = eliminateWayRow.ITEM_VALUE

        End If

        eliminateWayConditions.Clear()
        eliminateWayConditions.Add(AssetVisionXAuto.Facade.AfCIV.CIV_ELIMINATE_WAYColumns.ITEM_MEANINGColumn, " =", AssetVisionXService.Decls.DEFAULT_CI_TEXT_ELIMINATE_WAY_LOST)
        eliminateWayRow = AssetVisionXAuto.Facade.AfCIV.GetCIV_ELIMINATE_WAYRow(eliminateWayConditions)

        If IsNothing(eliminateWayRow) = True Then
            itemID = System.Guid.NewGuid.ToString()
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_LOST = _
                AssetVisionXService.Facade.OpSysConfig.CreateCommonItem( _
                    itemID, _
                    AssetVisionXAuto.Decls.CICNAME_ELIMINATE_WAY, _
                    AssetVisionXService.Decls.DEFAULT_CI_TEXT_ELIMINATE_WAY_LOST, _
                     String.Empty, _
                     True)
        Else
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_LOST = eliminateWayRow.ITEM_VALUE

        End If


        Dim inAccountCredenConditions As New AssetVisionXAuto.Facade.AfCIV.ConditionOfCIV_IN_ACCOUNT_CREDENCE_TEXT(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        inAccountCredenConditions.Add(AssetVisionXAuto.Facade.AfCIV.CIV_IN_ACCOUNT_CREDENCE_TEXTColumns.ITEM_MEANINGColumn, " =", AssetVisionXService.Decls.DEFAULT_CI_TEXT_IN_ACCOUNT_CREDENCE_SETTLED)

        Dim inAccountCredenRow As AssetVisionXAuto.FTs.FT_CIV_IN_ACCOUNT_CREDENCE_TEXTRow = _
            AssetVisionXAuto.Facade.AfCIV.GetCIV_IN_ACCOUNT_CREDENCE_TEXTRow(inAccountCredenConditions)

        If IsNothing(inAccountCredenRow) = True Then
            itemID = System.Guid.NewGuid.ToString()

            AssetVisionXService.Decls.DEFAULT_CI_VALUE_IN_ACCOUNT_CREDENCE_SETTLED = _
                AssetVisionXService.Facade.OpSysConfig.CreateCommonItem( _
                    itemID, _
                    AssetVisionXAuto.Decls.CICNAME_IN_ACCOUNT_CREDENCE_TEXT, _
                    AssetVisionXService.Decls.DEFAULT_CI_TEXT_IN_ACCOUNT_CREDENCE_SETTLED, _
                    String.Empty, _
                    True)

        Else
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_IN_ACCOUNT_CREDENCE_SETTLED = inAccountCredenRow.ITEM_VALUE
        End If


        Dim purchaseWayConditions As New AssetVisionXAuto.Facade.AfCIV.ConditionOfCIV_PURCHASE_WAY(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        purchaseWayConditions.Add(AssetVisionXAuto.Facade.AfCIV.CIV_PURCHASE_WAYColumns.ITEM_MEANINGColumn, " =", AssetVisionXService.Decls.DEFAULT_CI_TEXT_PURCHASE_WAY_PURCHASE)

        Dim purchaseWayCredenRow As AssetVisionXAuto.FTs.FT_CIV_PURCHASE_WAYRow = _
            AssetVisionXAuto.Facade.AfCIV.GetCIV_PURCHASE_WAYRow(purchaseWayConditions)

        If IsNothing(purchaseWayCredenRow) = True Then
            itemID = System.Guid.NewGuid.ToString()

            AssetVisionXService.Decls.DEFAULT_CI_VALUE_PURCHASE_WAY_PURCHASE = _
                AssetVisionXService.Facade.OpSysConfig.CreateCommonItem( _
                    itemID, _
                    AssetVisionXAuto.Decls.CICNAME_PURCHASE_WAY, _
                    AssetVisionXService.Decls.DEFAULT_CI_TEXT_PURCHASE_WAY_PURCHASE, _
                    String.Empty, _
                    True)

        Else
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_PURCHASE_WAY_PURCHASE = purchaseWayCredenRow.ITEM_VALUE
        End If

        Dim guaranteeExpireTransactionConditions As New AssetVisionXAuto.Facade.AfCIV.ConditionOfCIV_GUARANTEE_EXPIRE_TRANSACTION(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        guaranteeExpireTransactionConditions.Add(AssetVisionXAuto.Facade.AfCIV.CIV_GUARANTEE_EXPIRE_TRANSACTIONColumns.ITEM_MEANINGColumn, " =", AssetVisionXService.Decls.DEFAULT_CI_TEXT_GUARANTEE_EXPIRE_TRANSACTION_NONE)
        Dim guaranteeExpireTransactionRow = AssetVisionXAuto.Facade.AfCIV.GetCIV_GUARANTEE_EXPIRE_TRANSACTIONRow(guaranteeExpireTransactionConditions)

        If IsNothing(guaranteeExpireTransactionRow) = True Then
            itemID = System.Guid.NewGuid.ToString()
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_GUARANTEE_EXPIRE_TRANSACTION_NONE = _
                AssetVisionXService.Facade.OpSysConfig.CreateCommonItem( _
                    itemID, _
                    AssetVisionXAuto.Decls.CICNAME_GUARANTEE_EXPIRE_TRANSACTION, _
                    AssetVisionXService.Decls.DEFAULT_CI_TEXT_GUARANTEE_EXPIRE_TRANSACTION_NONE, _
                    String.Empty, _
                    True)
        Else
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_GUARANTEE_EXPIRE_TRANSACTION_NONE = guaranteeExpireTransactionRow.ITEM_VALUE

        End If

        Dim ownerTypeConditions As New AssetVisionXAuto.Facade.AfCIV.ConditionOfCIV_GUARANTEE_EXPIRE_TRANSACTION(XL.DB.Utils.Condition.LogicOperators.Logic_And)
        ownerTypeConditions.Add(AssetVisionXAuto.Facade.AfCIV.CIV_GUARANTEE_EXPIRE_TRANSACTIONColumns.ITEM_MEANINGColumn, " =", AssetVisionXService.Decls.DEFAULT_CI_TEXT_OWNER_TYPE_PROPRIETOR)
        Dim ownerTypeRow = AssetVisionXAuto.Facade.AfCIV.GetCIV_GUARANTEE_EXPIRE_TRANSACTIONRow(ownerTypeConditions)

        If IsNothing(ownerTypeRow) = True Then
            itemID = System.Guid.NewGuid.ToString()
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_OWNER_TYPE_PROPRIETOR = _
                AssetVisionXService.Facade.OpSysConfig.CreateCommonItem( _
                    itemID, _
                    AssetVisionXAuto.Decls.CICNAME_OWNER_TYPE, _
                    AssetVisionXService.Decls.DEFAULT_CI_TEXT_OWNER_TYPE_PROPRIETOR, _
                    String.Empty, _
                    True)
        Else
            AssetVisionXService.Decls.DEFAULT_CI_VALUE_OWNER_TYPE_PROPRIETOR = ownerTypeRow.ITEM_VALUE

        End If

        refDEFAULT_CI_VALUE_ELIMINATE_WAY_MAYBE_LOST = Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_MAYBE_LOST
        refDEFAULT_CI_VALUE_ELIMINATE_WAY_NONE = Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_NONE
        refDEFAULT_CI_VALUE_ELIMINATE_WAY_LOST = Decls.DEFAULT_CI_VALUE_ELIMINATE_WAY_LOST
        refDEFAULT_CI_VALUE_IN_ACCOUNT_CREDENCE_SETTLED = Decls.DEFAULT_CI_VALUE_IN_ACCOUNT_CREDENCE_SETTLED
        refDEFAULT_CI_VALUE_PURCHASE_WAY = Decls.DEFAULT_CI_VALUE_PURCHASE_WAY_PURCHASE
        refDEFAULT_CI_VALUE_GUARANTEE_EXPIRE_TRANSACTION = Decls.DEFAULT_CI_VALUE_GUARANTEE_EXPIRE_TRANSACTION_NONE
        refDEFAULT_CI_VALUE_OWNER_TYPE_PROPRIETOR = Decls.DEFAULT_CI_VALUE_OWNER_TYPE_PROPRIETOR
        '=================================================================================================================
        If SysInfo.RegisterVersionLevel = VersionLevel.Basic Then
            SysInfo.WriteShareSysInfo(Decls.SVN_ADDRESS_ALONE_DEPT_LEVEL, String.Empty)
        End If

        If SysInfo.RegisterVersionLevel <> VersionLevel.Enterprise AndAlso _
           SysInfo.RegisterVersionLevel <> VersionLevel.Trial Then
            SysInfo.WriteShareSysInfo(Decls.SVN_ASSET_OPERATE_WORKFLOW, String.Empty)
            SysInfo.WriteShareSysInfo(Decls.SVN_ASSET_KEEPER_ASSIGNABLE, String.Empty)
        End If

        refRegisterVersionLevelString = SysInfo.RegisterVersionLevel.ToString

        '当大部门级别>0时, 清除与Admin关联的用户
        If CommTK.FInteger(SysInfo.ReadShareSysInfo(Decls.SVN_ADDRESS_ALONE_DEPT_LEVEL)) > 0 Then
            Dim userCondition = New AssetVisionXAuto.Facade.AfSysConfig.ConditionOfS_USER(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            Dim userRowSE As New AssetVisionXAuto.FTs.FT_S_USERRowSEntity
            userCondition.Add(AssetVisionXAuto.Facade.AfSysConfig.S_USERColumns.USER_ACCOUNTColumn, "=", CommDecl.SYSTEM_DEFAULT_ACCOUNT)
            AssetVisionXAuto.Facade.AfSysConfig.FillS_USERRowSEntity(userRowSE, userCondition)

            Dim staffCondition = New AssetVisionXAuto.Facade.AfBizMaster.ConditionOfM_AV_STAFF(XL.DB.Utils.Condition.LogicOperators.Logic_And)
            staffCondition.Add(AssetVisionXAuto.Facade.AfBizMaster.M_AV_STAFFColumns.USER_IDColumn, "=", userRowSE.USER_ID)
            AssetVisionXAuto.Facade.AfBizMaster.ReviseM_AV_STAFFData(staffCondition, AssetVisionXAuto.Facade.AfBizMaster.M_AV_STAFFColumns.USER_IDColumn, String.Empty)
        ElseIf SysInfo.ReadShareSysInfo(AssetVisionXService.Decls.SVN_ADMIN_ALONE_STAFF_ID).Length > 0 Then
            SysInfo.WriteShareSysInfo(AssetVisionXService.Decls.SVN_ASSET_OPERATE_WORKFLOW, String.Empty)
        End If

        If CommTK.FInteger(SysInfo.ReadShareSysInfo(AssetVisionXService.Decls.SVN_ASSET_BARCODE_DIGIT_LENGTH)) < 6 Then
            SysInfo.WriteShareSysInfo(AssetVisionXService.Decls.SVN_ASSET_BARCODE_DIGIT_LENGTH, "6")
        End If

    End Sub

End Class
